package com.hyy.lotterysystem.service.mq;

import com.hyy.lotterysystem.common.utils.JacksonUtil;
import com.hyy.lotterysystem.controller.param.DrawPrizeParam;
import com.hyy.lotterysystem.dao.mapper.ActivityMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.hyy.lotterysystem.common.config.DelayedRabbitConfig.DELAYED_QUEUE_NAME;


/*
* 获取活动id
* 把活动中的状态改为结束*/
@Component
@RabbitListener(queues = DELAYED_QUEUE_NAME)
public class DelayedConsumer {
    private static final Logger logger = LoggerFactory.getLogger(DelayedConsumer.class);

    @Autowired
    private ActivityMapper activityMapper;

    @RabbitHandler
    public void process(Map<String, String> message) {
        logger.info("MQ成功接收到消息，message:{}",
                JacksonUtil.writeValueAsString(message));

        //把messageData（DrawPrizeParam对象的json字符串）转程Java对象
        String paramString = message.get("messageData");
        Integer activityId = JacksonUtil.readValue(paramString, Integer.class);
        long id=activityId;

        //根据活动id 把活动状态和活动关联的状态设置成complete
        activityMapper.activityComplete(id);

    }

}
